package com.owulia.leetcode.num;

import com.alibaba.fastjson.JSONObject;

public class SortedSquares {
    public static int[] sortedSquares(int[] nums) {
        int[] result = new int[nums.length];
        int left = 0;
        int right = nums.length - 1;
        int index = nums.length - 1;
        while (left <= right) {
            if (nums[left] * nums[left] >= nums[right] * nums[right]) {
                result[index--] = nums[left] * nums[left];
                left++;
            } else {
                result[index--] = nums[right] * nums[right];
                right--;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        /*int[] arr = {-7, -3, 2, 3, 11};
        System.out.println(JSONObject.toJSONString(sortedSquares(arr)));*/

        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        System.out.println(JSONObject.toJSONString(rotate(arr, 3)));
    }


    public static int[] rotate(int[] nums, int k) {
        int[] result = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            result[(i + k) % nums.length] = nums[i];
        }
        System.arraycopy(result,0,nums,0,nums.length);
        return result;
    }
}
